<template>
  <div>
    <el-form
      :inline="true"
      :model="formData"
      size="small"
      class="demo-form-inline"
    >
      <el-form-item prop="mobile" label="手机号码">
        <el-input
          v-model="formData.mobile"
          clearable
          placeholder="手机号码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="storeLevel" label="状态">
        <el-select v-model="formData.storeLevel" clearable placeholder="请选择">
          <el-option
            v-for="item in statusList"
            :key="item.status"
            :label="item.label"
            :value="item.status"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column prop="mobile" label="手机号码" width="180">
      </el-table-column>
      <el-table-column prop="userName" label="商家名称" width="180">
      </el-table-column>
      <el-table-column prop="nickName" label="商家昵称" width="180">
      </el-table-column>
      <el-table-column prop="storeLevel" label="是否商家" width="180">
        <template slot-scope="scope">
          {{ scope.row.storeLevel | storeLevel }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="mini"
            v-if="scope.row.storeLevel === 2"
            @click="
              StoreLevelAudit(scope.row.userSn, 1, '是否修改该用户为商家？')
            "
            >同意</el-button
          >
          <el-button
            type="danger"
            size="mini"
            v-if="scope.row.storeLevel === 2"
            @click="
              StoreLevelAudit(scope.row.userSn, 0, '是否把商家改为用户？')
            "
            >拒绝</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <v-pagination :formData="formData" @load="getData"></v-pagination>
  </div>
</template>
<script>
import {
  storeLevelList,
  storeLevelAudit,
} from "@/api/auditManagement/storeLevelAudit";
import pagination from "@/components/pagination";
export default {
  components: {
    vPagination: pagination,
  },
  data() {
    return {
      loading: false,
      tableData: [],
      formData: {
        mobile: "",
        storeLevel: "",
        pageNo: 1,
        pageSize: 10,
        total: 0,
      },
      statusList: [
        {
          label: "全部",
          status: "",
        },
        {
          label: "未提交",
          status: 0,
        },
        {
          label: "通过",
          status: 1,
        },
        {
          label: "待审核",
          status: 2,
        },
      ],
    };
  },
  filters: {
    storeLevel(status) {
      switch (status) {
        case 0:
          return "否";
          break;
        case 1:
          return "是";
          break;
        case 2:
          return "待审核";
          break;
        default:
          return status;
          break;
      }
    },
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      this.loading = true;
      storeLevelList(this.formData).then((res) => {
        this.loading = false;
        this.tableData = res.data.resultData.list;
        this.formData.total = res.data.resultData.count;
      });
    },
    onSubmit() {
      this.formData.pageNo = 1;
      this.formData.pageSize = 10;
      this.formData.total = 0;
      this.getData();
    },
    StoreLevelAudit(userSn, storeLevel, message) {
      this.$msgbox({
        title: storeLevel === 1 ? "通过" : "拒绝",
        message: message,
        showCancelButton: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        beforeClose: (action, instance, done) => {
          if (action === "confirm") {
            var data = {
              userSn: userSn,
              storeLevel: storeLevel,
            };
            instance.confirmButtonLoading = true;
            storeLevelAudit(data).then((res) => {
              instance.confirmButtonLoading = false;
              if (res.data.resultStatus) {
                done();
                this.getData();
                this.$message.success(res.data.errorMessage || "修改成功");
              }
            });
          } else {
            done();
          }
        },
      }).catch(() => {});
    },
  },
};
</script>